// src/router/index.js
import { createRouter, createWebHistory } from 'vue-router'
import { getToken } from '@/utils/auth'
const Layout = () => import('@/layout/index.vue')

const routes = [
    {
        path: '/video',
        component: Layout,
        meta: { title: '视频管理', icon: 'VideoCamera' },
        children: [
            {
                path: 'article',
                name: 'VideoArticle',
                component: () => import('@/views/video/article/index.vue'),
                meta: { title: '文章管理' }
            },
            {
                path: 'task',
                name: 'VideoTask',
                component: () => import('@/views/video/task/index.vue'),
                meta: { title: '视频任务' }
            },
            {
                path: 'config',
                name: 'VideoConfig',
                component: () => import('@/views/video/config/index.vue'),
                meta: { title: '视频配置' }
            },
            {
                path: 'template',
                name: 'TemplateConfig',
                component: () => import('@/views/video/template/template.vue'),
                meta: { title: '视频模板' }
            },
            {
                path: 'avatar',
                name: 'AvatarList',
                component: () => import('@/views/video/avatar/index.vue'),
                meta: { title: '虚拟人物' }
            },
            {
                path: 'voice',
                name: 'VoiceList',
                component: () => import('@/views/video/voice/index.vue'),
                meta: { title: '语音管理' }
            }
        ]
    },
    {
        path: '/login',
        name: 'Login',
        component: () => import('@/views/auth/Login.vue'),
        meta: { title: '登录', noAuth: true }
    },
    {
        path: '/register',
        name: 'Register',
        component: () => import('@/views/auth/Register.vue'),
        meta: { title: '注册', noAuth: true }
    },
    {
        path: '/',
        component: Layout,
        redirect: '/dashboard',
        children: [
            {
                path: 'dashboard',
                name: 'Dashboard',
                component: () => import('@/views/dashboard/index.vue'),
                meta: { title: '控制台', icon: 'DataBoard' }
            }
        ]
    },
    {
        path: '/crawler',
        component: Layout,
        meta: { title: '爬虫管理', icon: 'Connection' },
        children: [
            {
                path: 'tasks',
                name: 'CrawlerTasks',
                component: () => import('@/views/crawler/TaskPage.vue'),
                meta: { title: '爬虫任务管理' }
            },
            {
                path: 'articles',
                name: 'CrawlerArticles',
                component: () => import('@/views/crawler/ArticlePage.vue'),
                meta: { title: '爬虫文章管理' }
            }
        ]
    },
    {
        path: '/system',
        component: Layout,
        children: [
            {
                path: 'setting',
                name: 'System',
                component: () => import('@/views/system/index.vue'),
                meta: { title: '系统设置', icon: 'Setting' }
            },
            {
                path: 'background',
                name: 'BackgroundSystem',
                component: () => import('@/views/background/BackgroundPage.vue'),
                meta: { title: '图片视频资源管理', icon: 'Photo' }
            },
            {
                path: 'category',
                name: 'SystemCategory',
                component: () => import('@/views/category/index.vue'),
                meta: { title: '分类管理', icon: 'Setting' }
            }
        ]
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
    const token = getToken()

    if (token) {
        if (to.path === '/login') {
            // 已登录且要跳转的页面是登录页
            next('/')
        } else {
            next()
        }
    } else {
        if (to.meta.noAuth) {
            // 未登录但访问无需认证的页面
            next()
        } else {
            // 未登录且要跳转的页面需要认证
            next(`/login?redirect=${to.path}`)
        }
    }
})

export default router